package com.ruoyi.web.controller.lowcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lowcode.domain.vo.LcDatabaseSourceVo;
import com.ruoyi.lowcode.domain.bo.LcDatabaseSourceBo;
import com.ruoyi.lowcode.service.ILcDatabaseSourceService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据源列
 *
 * @author ruoyi
 * @date 2023-09-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lowcode/databaseSource")
public class LcDatabaseSourceController extends BaseController {

    private final ILcDatabaseSourceService iLcDatabaseSourceService;

    /**
     * 查询数据源列列表
     */
    @SaCheckPermission("lowcode:databaseSource:list")
    @GetMapping("/list")
    public TableDataInfo<LcDatabaseSourceVo> list(LcDatabaseSourceBo bo, PageQuery pageQuery) {
        return iLcDatabaseSourceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出数据源列列表
     */
//    @SaCheckPermission("lowcode:databaseSource:export")
//    @Log(title = "数据源列", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(LcDatabaseSourceBo bo, HttpServletResponse response) {
//        List<LcDatabaseSourceVo> list = iLcDatabaseSourceService.queryList(bo);
//        ExcelUtil.exportExcel(list, "数据源列", LcDatabaseSourceVo.class, response);
//    }

    /**
     * 获取数据源列详细信息
     *
     * @param databaseSourceId 主键
     */
    @SaCheckPermission("lowcode:databaseSource:query")
    @GetMapping("/{databaseSourceId}")
    public R<LcDatabaseSourceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long databaseSourceId) {
        return R.ok(iLcDatabaseSourceService.queryById(databaseSourceId));
    }

    /**
     * 新增数据源列
     */
    @SaCheckPermission("lowcode:databaseSource:add")
    @Log(title = "数据源列", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LcDatabaseSourceBo bo) {
        return toAjax(iLcDatabaseSourceService.insertByBo(bo));
    }

    /**
     * 修改数据源列
     */
    @SaCheckPermission("lowcode:databaseSource:edit")
    @Log(title = "数据源列", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LcDatabaseSourceBo bo) {
        return toAjax(iLcDatabaseSourceService.updateByBo(bo));
    }

    /**
     * 删除数据源列
     *
     * @param databaseSourceIds 主键串
     */
    @SaCheckPermission("lowcode:databaseSource:remove")
    @Log(title = "数据源列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{databaseSourceIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] databaseSourceIds) {
        return toAjax(iLcDatabaseSourceService.deleteWithValidByIds(Arrays.asList(databaseSourceIds), true));
    }

    @GetMapping(value = "/type")
    public R<List<LcDatabaseSourceVo>> dictType() {
        List<LcDatabaseSourceVo> data = iLcDatabaseSourceService.selectDictDataByType();
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return R.ok(data);
    }

}
